﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.EventSystems;

public class PackagePanel : MonoBehaviour
{
    public Vector2 LeftItemFromandToPos;
    public Vector2 RightFromandToPos;

    public RectTransform LeftItem;
    public RectTransform RightItem;

    public List<ItemBase> Items = new List<ItemBase>();


    public GameObject itemTemp;
    public GameObject itemTempParent;

    #region  right data

    public Text RightName;
    public Text RightDesc;
    

    #endregion


    #region  构建假的ITem数据

    private void CtreateItems()
    {
        for (int i =0;i <20;i++)
        {
            ItemBase item = new ItemBase("item_" + i.ToString(), "this is item_ " + i.ToString(), "item_" + i.ToString() ,Random.Range(1,5).ToString(), Random.Range(0,1));
            Items.Add(item);
        }
    }


    #endregion



    private void Awake()
    {
        LeftItem.localPosition = new Vector3(LeftItemFromandToPos.x, LeftItem.localPosition.y, LeftItem.localPosition.z);
        //RightItem.gameObject.SetActive(false);
        RightItem.localPosition = new Vector3(RightFromandToPos.x, RightItem.localPosition.y, RightItem.localPosition.z);
        this.CtreateItems();

        this.ShowItems();
    }



    // Use this for initialization
    void Start ()
    {
        Tweener leftitemtween = LeftItem.DOLocalMoveX(LeftItemFromandToPos.y, 0.5f).OnStart(()=>
        {
            //LeftItem.localPosition = new Vector3(LeftItemFromandToPos.x, LeftItem.localPosition.y, LeftItem.localPosition.z);
            LeftItem.gameObject.SetActive(true);
        });
        Tweener rightitemtween = RightItem.DOLocalMoveX( RightFromandToPos.y,0.5f).OnStart(() =>
        {
            //RightItem.localPosition = new Vector3(RightFromandToPos.x, RightItem.localPosition.y, RightItem.localPosition.z);
            RightItem.gameObject.SetActive(true);
        }); ;
        leftitemtween.SetAutoKill(false);
        //leftitemtween.Pause();
        rightitemtween.SetAutoKill(false);
        rightitemtween.Pause();
    }

    [ContextMenu("DoLeftItemMoveAnim")]
    private void DoLeftItemMoveAnim()
    {
        if (true)
        {
            LeftItem.DOPlayForward();
        }
        else
        {
            LeftItem.DOPlayBackwards();
        }
    }

    private void DoRightItemAnim(bool enable = true)
    {
        if (enable)
        {
            RightItem.DOPlayForward();
        }
        else
        {
            RightItem.DOPlayBackwards();
        }
    }



    public void ShowItems()
    {
        for (int i =0;i< Items.Count;i++)
        {
            GameObject item = Instantiate(this.itemTemp,this.itemTempParent.transform) as GameObject;
            var script =  item.GetComponent<ItemData>();
            script.SetData(null, Items[i].itemCount,i);// Items[i].itemIcon,
            item.SetActive(true);
            item.name = "item_" + i.ToString();
            var btn =  item.GetComponent<Button>();

            if (btn != null)
            {
                btn.onClick.AddListener(()=>
                {
                    //1 显示右边你的界面
                    //2 并且显示出具体的Item 的信息
                    Debug.LogError("xxxxxxxxxxxxxxx");
                    //1 
                    this.DoRightItemAnim();
                    //2
                    int xx = btn.GetComponent<ItemData>().index;
                    ItemBase baseData = Items[xx];

                    this.RightName.text = baseData.itemName;
                    this.RightDesc.text = baseData.itemDesc;
                });
            }
        }
    }




	
}
